package org.grow.stable.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.grow.stable.security.domain.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

@Service
public class AuthHandler implements AuthenticationFailureHandler
        ,AuthenticationSuccessHandler
        ,LogoutSuccessHandler {
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        System.out.println("登录失败 "+httpServletRequest.getParameter("username")+"===>>"+httpServletRequest.getParameter("password"));
        System.out.println(passwordEncoder.encode(httpServletRequest.getParameter("password")));
        httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        httpServletResponse.getWriter().write("sorry,用户名密码不正确");

    }


    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        System.out.println("登录成功");
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
//        httpServletResponse.getWriter()
//                .write(objectMapper.writeValueAsString(auth));
        HashMap<String, Object> results = new HashMap<>();
        User authPrincipal = (User) auth.getPrincipal();

        results.put("username",authPrincipal.getUsername());
        results.put("nickname",authPrincipal.getNickname());

        try(PrintWriter writer = httpServletResponse.getWriter()){
            writer.write(objectMapper.writeValueAsString(results));
        }
    }

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
       // String s = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();

        System.out.println(" 登出成功");
        httpServletResponse.getWriter().write("登出成功");
    }
}
